package cm.tfd.hospital.util;

import android.text.TextUtils;

import com.google.gson.Gson;

import org.json.JSONObject;

/**
 * Created by yuye on 2018/5/24.
 * json工具类
 */

public class JsonUtils {
    private static Gson mGson;

    private JsonUtils() {
    }

    public static boolean   isJsonOk(String json) {
        boolean isOk = false;
        try {
            JSONObject jsonObject = new JSONObject(json);
            int ret = jsonObject.optInt("ret");
            String msg = jsonObject.optString("msg");
            isOk = ret == 200;
            if (!isOk && !TextUtils.isEmpty(msg)) {
//                ToastUtils.show(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isOk;
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        T obj = null;
        try {
            if (mGson == null) {
                mGson = new Gson();
            }
            obj = mGson.fromJson(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static String toJson(Object obj) {
        String json = "";
        try {
            if (mGson == null) {
                mGson = new Gson();
            }
            json = mGson.toJson(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
